﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace haptic.src.engine.threads
{
    public class Worker
    {
        #region Constructors
        public Worker(JobManager oJobManager, int iId)
        {
            m_oJobManager = oJobManager;
            m_oThread = new Thread(Run);
            m_oThread.Name = "worker " + iId;
        }
        #endregion

        #region Actions
        public void Start()
        {
            m_oThread.Start();
        }

        public void Abort()
        {
            m_oThread.Abort();
        }
        #endregion

        private void Run()
        {
            while(true)
            {
                JobManager.m_oWorkerReady.Release();
                m_oJobManager.m_oWorkerGo.WaitOne();
                m_oJob = m_oJobManager.m_oPendingJob;
                m_oJobManager.m_oWorkerGotJob.Set();
                m_oJob.Compute();
            }
        }

        #region Attributes
        private Job m_oJob;
        private JobManager m_oJobManager;
        private Thread m_oThread;
        #endregion
    }
}
